我看 ZOJ 題目寫的,原文好像沒看到,但大家好像都會特別轉小寫???
gets()
或其他可忽略空白之寫法來讀取,並存到字元陣列
gets()
是個不安全的函式,但...反正解題嘛
char str[999] = {0};
gets(str);
for
迴圈去逐字元檢查 & 解密switch
工人智慧版
for(i = 0; i < strlen(str); i++){
switch(str[i]){
case 32:
printf(" ");
break;
case 'e':
printf("q");
break;
case 'd':
printf("a");
break;
case 'c':
printf("z");
break;
case 'r':
printf("w");
break;
case 'f':
printf("s");
break;
case 'v':
printf("x");
break;
case 't':
printf("e");
break;
case 'g':
printf("d");
break;
case 'b':
printf("c");
break;
case 'y':
printf("r");
break;
case 'h':
printf("f");
break;
case 'n':
printf("v");
break;
case 'u':
printf("t");
break;
case 'j':
printf("g");
break;
case 'm':
printf("b");
break;
case 'i':
printf("y");
break;
case 'k':
printf("h");
break;
case ',':
printf("n");
break;
case 'o':
printf("u");
break;
case 'l':
printf("j");
break;
case '.':
printf("m");
break;
case 'p':
printf("i");
break;
case ';':
printf("k");
break;
case '/':
printf(",");
break;
case '[':
printf("o");
break;
case '\'':
printf("l");
break;
case ']':
printf("p");
break;
case '\\':
printf("[");
break;
case '=':
printf("0");
break;
case '-':
printf("9");
break;
case '0':
printf("8");
break;
case '9':
printf("7");
break;
case '8':
printf("6");
break;
case '7':
printf("5");
break;
case '6':
printf("4");
break;
case '5':
printf("3");
break;
case '4':
printf("2");
break;
case '3':
printf("1");
break;
case '2':
printf("`");
break;
}
}
1.
的表,找到就輸出往前第二個字元char table[ ] = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
...
for(j = 0; j < strlen(table); j++){
if(str[i] == table[j]){
printf("%c", table[j - 2]);
break;
}
}
char ct[ ] = " 234567890-=ertyuiop[]\\dfghjkl;'cvbnm,./";
char pt[ ] = " `1234567890qwertyuiop[asdfghjklzxcvbnm,";
#include<stdio.h>
#include<string.h>
int main(){
char str[999] = {0};
char ct[ ] = " 234567890-=ertyuiop[]\\dfghjkl;'cvbnm,./";
char pt[ ] = " `1234567890qwertyuiop[asdfghjklzxcvbnm,";
int i, j;
gets(str);
for(i = 0; i < strlen(str); i++){
for(j = 0; j < strlen(ct); j++){
if(str[i] == ct[j]){
printf("%c", pt[j]);
}
}
}
printf("\n");
return 0;
}
string
來處理字串string::find
更簡短#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
string table = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
getline(cin, str);
for(int i = 0; i < str.size(); i++){
if(str[i] == ' '){
cout << " ";
}
else{
cout << table[table.find(str[i]) - 2];
}
}
cout << "\n";
}